/*
 * UpdateFileTaskFacade.java
 *
 * Copyright(c) 2011
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.update;

import android.content.Context;
import android.util.Log;
import de.studiorutton.access.Result;
import de.studiorutton.gerengdic.LearningWordsApplication;
import de.studiorutton.gerengdic.library.LibraryDatabase;

public class UpdateFileTaskFacade {
	private final static String TAG = "learningwords";
	
	private UpdateFile uf;
	private UpdateFileTask uft;
	private MyUpdateTaskProgressListener mutpl;
	private MyUpdateTaskEventListener mutel;
	private Context ctx;
	private int titleRessource;

	private int progress;
	private String progressString;

	public UpdateFileTaskFacade(int libraryFile, int titleRessource) {
		this.titleRessource = titleRessource;
		LibraryDatabase ld = LibraryDatabase.get();
		ctx = LearningWordsApplication.get();

		uf = new UpdateFile(ld.getFileLocation(libraryFile));
		mutpl = new MyUpdateTaskProgressListener(null);
		mutel = new MyUpdateTaskEventListener(null);
		progress = 0;
		progressString = "0/0 KB";
	}

	public int getTitleResource() {
		return titleRessource;
	}

	public UpdateFile getUpdateFile() {
		return uf;
	}

	public int getProgress() {
		return progress;
	}

	public String getProgressString() {
		return progressString;
	}

	public MyUpdateTaskProgressListener getUpdateTaskProgressListener() {
		return mutpl;
	}

	public MyUpdateTaskEventListener getUpdateTaskEventListener() {
		return mutel;
	}

	public void execute( boolean checkForNewer ) {
		uft = new UpdateFileTask(ctx, uf, checkForNewer );
		uft.setCallbacks(mutpl, mutel);
		uft.disableDialog();
		uft.execute();
	}

	public void cancel(boolean interrupt) {
		if (uft != null)
			uft.cancel(interrupt);
	}

	public void clearTask() {
		uft = null;
	}

	class MyUpdateTaskProgressListener implements IUpdateTaskProgressListener {
		IUpdateTaskProgressListener utpl;

		public MyUpdateTaskProgressListener(IUpdateTaskProgressListener utpl) {
			this.utpl = utpl;
		}

		public void notifyProgress(int progress, long current, long total) {
			UpdateFileTaskFacade.this.progress = progress / 10;
			progressString = (current / 1000) + "/" + (total / 1000) + " KB";
			utpl.notifyProgress(progress, current, total);

		}

		public void setProgressListener(IUpdateTaskProgressListener utpl) {
			this.utpl = utpl;
		}
	}

	class MyUpdateTaskEventListener implements IUpdateTaskEventListener {
		IUpdateTaskEventListener utel;

		public MyUpdateTaskEventListener(IUpdateTaskEventListener utel) {
			this.utel = utel;
		}

		public void notifyTaskEnded(Result res) {
			if (res.isError()) {
//				Log.d( TAG, "MyUpdateTaskEventListener:notifyTaskEnded: res.isError = true");
				progress = 0;
				progressString = "0/0 KB";
			}
			mutpl.notifyProgress(0, 0, 0);
			utel.notifyTaskEnded(res);
		}

		public void setEventListener(IUpdateTaskEventListener utel) {
			this.utel = utel;
		}

		public void notifyNewer(Result res) {
			utel.notifyNewer( res );
			
		}
		
	}

}
